package com.weike.foodsafe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weike.common.utils.JwtHelper;
import com.weike.common.utils.Local;
import com.weike.common.utils.PageUtils;
import com.weike.common.utils.Query;
import com.weike.foodsafe.dao.AddressDao;
import com.weike.foodsafe.dao.DistributionDao;
import com.weike.foodsafe.dao.InventoryDao;
import com.weike.foodsafe.dao.PurchaserDao;
import com.weike.foodsafe.entity.AddressEntity;
import com.weike.foodsafe.entity.InventoryEntity;
import com.weike.foodsafe.entity.PurchaserEntity;
import com.weike.foodsafe.entity.SupplierEntity;
import com.weike.foodsafe.service.AddressService;
import com.weike.foodsafe.service.InventoryService;
import com.weike.foodsafe.service.SupplierService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("inventoryService")
public class InventoryServiceImpl extends ServiceImpl<InventoryDao, InventoryEntity> implements InventoryService {

    @Autowired
    private Local local;

    @Autowired
    private DistributionDao distributionDao;

    @Autowired
    private SupplierService supplierService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<InventoryEntity> page = this.page(
                new Query<InventoryEntity>().getPage(params),
                new QueryWrapper<InventoryEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 获取库存列表
     * @param params
     * @return
     */
    @Override
    public PageUtils inventoryList(Map<String, Object> params) {
        String currentUserId = local.getCurrentUserId();
        String key = (String)params.get("key");
        String status = (String)params.get("status");

        LambdaQueryWrapper<InventoryEntity> wrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isBlank(key)) {
            wrapper.like(InventoryEntity :: getInventoryName , key);
        }

        if (!StringUtils.isBlank(key)) {
            wrapper.eq(InventoryEntity :: getIsInventory , status);
        }
        String distributionIdByUserId = distributionDao.getDistributionIdByUserId(currentUserId);
        wrapper.eq(InventoryEntity :: getDistributionId , distributionIdByUserId);
        IPage<InventoryEntity> page = this.page(
                new Query<InventoryEntity>().getPage(params),
                wrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        List<InventoryEntity> records = page.getRecords();
        List<String> supplierId = records.stream().map(InventoryEntity::getInventoryId).filter(Objects::nonNull).collect(Collectors.toList());

        if (supplierId.size() > 0) {
            List<SupplierEntity> supplierEntities = supplierService.listByIds(supplierId);

            records.stream().filter(inventoryEntity
                            -> inventoryEntity.getSupplierId() != null)
                    .forEach(inventoryEntity -> {
                        List<SupplierEntity> supplierEntityList = supplierEntities.stream().filter(supplierEntity ->
                                        supplierEntity.getSupplierid().equals(inventoryEntity.getSupplierId()))
                                .collect(Collectors.toList());
                        if (supplierEntityList.size() > 0) {
                            inventoryEntity.setSupplierName(supplierEntityList.get(0).getSupplierCompanyName());
                        }

                    });
        }


        return pageUtils;
    }

}